//
//  TTMusicSinglePlayerViewController.m
//  TTMusic
//
//  Created by yanghuiyan on 16/4/7.
//  Copyright © 2016年 cpy. All rights reserved.
//

#import "TTMusicSinglePlayerViewController.h"
#import "TTMusicAVPlayer.h"
#import "UMSocial.h"
#import "CPYDiscoveryPageSongListModel.h"
#import "CPYMVPlayViewController.h"
#import "CPYDiscoveryPageUrlListModel.h"
#import "CPYMVPlayViewController.h"
@interface TTMusicSinglePlayerViewController ()<UMSocialUIDelegate>

@property (nonatomic, strong) AVPlayerItem *item;

@property (nonatomic, assign) BOOL isLike;


@property (nonatomic, assign) BOOL isSongList;

@property (nonatomic, assign) BOOL isRow;

@property (nonatomic, strong) UIButton *loadButton;

@property (nonatomic, strong) UIButton *favoriteButton;

@property (nonatomic, strong) UIButton *shareButton;

@property (nonatomic, strong) UISlider *slider;

@property (nonatomic, strong) UIButton *mvButton;

@property (nonatomic, strong) UIButton *typeButton;

@property (nonatomic, strong) UIButton *listButton;

@property (nonatomic, strong) UIButton *playButton;

@property (nonatomic, strong) UIButton *nextSongButton;

@property (nonatomic, strong) UIButton *beforeSongButton;
@property (nonatomic, strong) NSTimer *sliderTimer;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) UILabel *currentTimeLabel;
@property (nonatomic, strong) UILabel *wholeTimeLabel;
@property (nonatomic, strong) NSString *timeStr;
@property (nonatomic, strong) NSString *timeStr1;
@property (nonatomic, strong) CAGradientLayer *grandientLayer;
@property (nonatomic, strong) CPYDataBaseTool *dataBase;
@end

@implementation TTMusicSinglePlayerViewController

+ (instancetype)sharePlayViewController  {
    static TTMusicSinglePlayerViewController *playViewController = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        playViewController = [[TTMusicSinglePlayerViewController alloc] init];
        
    });
    return playViewController;
}

- (void)viewWillAppear:(BOOL)animated {
    self.dataBase = [CPYDataBaseTool dataBaseManager];

    [_dataBase openDataBase];
    [_dataBase createTable];
    NSLog(@"yuyuy%d", [_dataBase selectDataBaseWithId:[NSString stringWithFormat:@"%@", [TTMusicAVPlayer shareTTMusicAVPlayer].songDetailModel.songId]]);
    if ([_dataBase selectDataBaseWithId:[NSString stringWithFormat:@"%@", [TTMusicAVPlayer shareTTMusicAVPlayer].songDetailModel.songId]]) {
        [_favoriteButton setImage:[[UIImage imageNamed:@"VCLIke"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        _isLike = YES;
    }else {
        
        [_favoriteButton setImage:[[UIImage imageNamed:@"VCUnLike"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        _isLike = NO;
    }
    [_dataBase closeDataBase];
    [super viewWillAppear:YES];
    [self imageRound];
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor cyanColor];
//    播放条发出的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeCondition:) name:@"changeCondition" object:nil];
    
    [self creatView];
}
#pragma mark - 切换播放状态
- (void)changeCondition:(NSNotification *)notification {
    [TTMusicAVPlayer shareTTMusicAVPlayer].isPlay = notification.object;
    if ([notification.object isEqual:@1]) {
        [self.playButton setBackgroundImage:[[UIImage imageNamed:@"VCPlay"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }else {
        [self.playButton setBackgroundImage:[[UIImage imageNamed:@"VCStop"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
}
#pragma mark - 初始化
- (instancetype)init {
    self = [super init];
    if (self) {
        //    歌曲名
        self.songLabel = [[TTMusicBaseLabel alloc] initWithFrame:CGRectMake(50 * FitWidth, 30 * FitHeight, 200 * FitWidth, 30 * FitHeight)];
        _songLabel.font = [UIFont boldSystemFontOfSize:20];
        _songLabel.textColor = [UIColor whiteColor];
        _songLabel.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:_songLabel];
        //    歌手名
        self.singerLabel = [[TTMusicBaseLabel alloc] initWithFrame:CGRectMake(50 * FitWidth, 65 * FitHeight, 200 * FitWidth, 20 * FitHeight)];
        _singerLabel.textColor = [UIColor whiteColor];
        _singerLabel.textAlignment = NSTextAlignmentCenter;
        _singerLabel.font = [UIFont boldSystemFontOfSize:17];
        [self.view addSubview:_singerLabel];
        self.controllImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x - (SysWidth - 140) / 2, self.view.center.y - 240, 150, 20)];
        _controllImage.image = [UIImage imageNamed:@"controll"];
        [self.view addSubview:_controllImage];
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x - (SysWidth - 140) / 2, self.view.center.y - 200, SysWidth - 140, SysWidth - 140)];
        _imageView.layer.masksToBounds = YES;
        _imageView.layer.cornerRadius = (SysWidth - 140) / 2;
        _imageView.userInteractionEnabled = YES;
        
        [self.view addSubview:_imageView];
        self.dataBase = [CPYDataBaseTool dataBaseManager];
    }
    return self;
}

#pragma mark - creatView
- (void)creatView {
    TTMusicBaseView *playView = [[TTMusicBaseView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 200, SysWidth, 200)];
    playView.backgroundColor = [UIColor clearColor];
    playView.alpha = 0.5;
    [self.view addSubview:playView];
    //设置左侧返回按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(10, 30, 40, 40);
    [button setImage:[[UIImage imageNamed:@"VCReturn"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];
    [self.view addSubview:button];
//    下载
    self.loadButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _loadButton.frame = CGRectMake((SysWidth - 120) / 5, 7, 30 * FitWidth, 30 * FitHeight);
    [_loadButton setImage:[[UIImage imageNamed:@"VCDown"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [_loadButton addTarget:self action:@selector(loadButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [playView addSubview:_loadButton];
//    喜欢（收藏）
    self.favoriteButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _favoriteButton.frame = CGRectMake((SysWidth - 120) / 5 * 2 + 30, 7, 30 * FitWidth, 30 * FitHeight);
//    CPYDataBaseTool *dataBase = [CPYDataBaseTool dataBaseManager];
    [_dataBase openDataBase];
    [_dataBase createTable];
//    先判断是否收藏
    if ([_dataBase selectDataBaseWithId:[NSString stringWithFormat:@"%@", [TTMusicAVPlayer shareTTMusicAVPlayer].songDetailModel.songId]]) {
        [_favoriteButton setImage:[[UIImage imageNamed:@"VCLIke"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        _isLike = YES;
    }else {
        
        [_favoriteButton setImage:[[UIImage imageNamed:@"VCUnLike"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        _isLike = NO;
    }
    [_dataBase closeDataBase];
    [_favoriteButton addTarget:self action:@selector(favoriteButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [playView addSubview:_favoriteButton];
//    分享
    self.shareButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _shareButton.frame = CGRectMake((SysWidth - 120) / 5 * 3 + 60, 7, 30 * FitWidth, 30 * FitHeight);
    [_shareButton setImage:[[UIImage imageNamed:@"VCShare"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [_shareButton addTarget:self action:@selector(shareButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [playView addSubview:_shareButton];
//    播放MV
    self.mvButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _mvButton.frame = CGRectMake((SysWidth - 120) / 5 * 4 + 90, 7, 30 * FitWidth, 30 * FitHeight);
    [_mvButton setImage:[[UIImage imageNamed:@"MV"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [_mvButton addTarget:self action:@selector(mvButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [playView addSubview:_mvButton];
//    播放当前时间
    self.currentTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 45, 40, 30)];
    _currentTimeLabel.textColor = [UIColor blueColor];
    _currentTimeLabel.font = [UIFont systemFontOfSize:13];
    [playView addSubview:_currentTimeLabel];
//    进度条
    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(50, 45, SysWidth - 100, 30)];
    _slider.minimumValue = 0.0f;
    _slider.maximumValue = 1.0f;
    [_slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    [playView addSubview:_slider];
//    总时间
    self.wholeTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_slider.frame.origin.x + _slider.bounds.size.width + 5, 45, 40, 30)];
    _wholeTimeLabel.font = [UIFont systemFontOfSize:13];
    _wholeTimeLabel.textColor = [UIColor blueColor];
    [playView addSubview:_wholeTimeLabel];
    self.sliderTimer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(observePlay) userInfo:nil repeats:YES];
//    播放模式
    self.style = [TTMusicAVPlayer shareTTMusicAVPlayer].style;

    self.typeButton = [UIButton buttonWithType:UIButtonTypeSystem];
    
    _typeButton.frame = CGRectMake(20, _loadButton.frame.origin.y + 93, 35 * FitWidth, 35 * FitHeight);
    
    if ([TTMusicAVPlayer shareTTMusicAVPlayer].style == 0) {
        [self.typeButton setBackgroundImage:[[UIImage imageNamed:@"VCShunxu"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
    if ([TTMusicAVPlayer shareTTMusicAVPlayer].style == 1) {
        [self.typeButton setBackgroundImage:[[UIImage imageNamed:@"VCDanqu"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
    if ([TTMusicAVPlayer shareTTMusicAVPlayer].style == 2) {
        [self.typeButton setBackgroundImage:[[UIImage imageNamed:@"VCSuiji"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
    [_typeButton addTarget:self action:@selector(typeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [playView addSubview:_typeButton];
//    上一曲
    self.beforeSongButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _beforeSongButton.frame = CGRectMake((SysWidth - 220) / 6 + 64, _loadButton.frame.origin.y + 93, 35 * FitWidth, 35 * FitHeight);
    [_beforeSongButton setImage:[[UIImage imageNamed:@"VCBdforeSong"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [_beforeSongButton addTarget:self action:@selector(beforeSongButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [playView addSubview:_beforeSongButton];
//    播放／暂停
    self.playButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _playButton.frame = CGRectMake(SysWidth / 2 - 45, _typeButton.frame.origin.y - 19, 90 * FitWidth, 90 * FitHeight);
    if ([TTMusicAVPlayer shareTTMusicAVPlayer].isPlay) {
        [self.playButton setBackgroundImage:[[UIImage imageNamed:@"VCStop"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [self imageRound];
        [TTMusicAVPlayer shareTTMusicAVPlayer].isPlay = YES;
    } else {
        [self.playButton setBackgroundImage:[[UIImage imageNamed:@"VCPlay"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [self imageRound];
        [TTMusicAVPlayer shareTTMusicAVPlayer].isPlay = NO;
    }
    [_playButton addTarget:self action:@selector(playAndStopButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [playView addSubview:_playButton];
//    下一曲
    self.nextSongButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _nextSongButton.frame = CGRectMake(SysWidth - (SysWidth - 220) / 6 - 98, _loadButton.frame.origin.y + 93, 35 * FitWidth, 35 * FitHeight);
    [_nextSongButton setImage:[[UIImage imageNamed:@"VCNextSon"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [_nextSongButton addTarget:self action:@selector(nextSongButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [playView addSubview:_nextSongButton];
//    播放列表
    self.listButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _listButton.frame = CGRectMake(SysWidth - 55, _typeButton.frame.origin.y, 35 * FitWidth, 35 * FitHeight);
    [_listButton setImage:[[UIImage imageNamed:@"VCList"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [_listButton addTarget:self action:@selector(listButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [playView addSubview:_listButton];
}
- (void)updateSlider {
    // Update the slider about the music time
    self.slider.value = CMTimeGetSeconds([TTMusicAVPlayer shareTTMusicAVPlayer].currentItem.currentTime) / CMTimeGetSeconds([TTMusicAVPlayer shareTTMusicAVPlayer].currentItem.duration);
    //总时间
    NSInteger totalTime = CMTimeGetSeconds([TTMusicAVPlayer shareTTMusicAVPlayer].currentItem.duration);
    NSInteger minTime = totalTime / 60;
    NSInteger secondTime = totalTime % 60;
    _timeStr = [NSString stringWithFormat:@"%02ld:%02ld", (long)minTime,(long)secondTime];
    //开始时间
    NSInteger totalTime1 = CMTimeGetSeconds([TTMusicAVPlayer shareTTMusicAVPlayer].currentItem.currentTime);
    NSInteger minTime1 = totalTime1 / 60;
    NSInteger secondTime1 = totalTime1 % 60;
    _timeStr1 = [NSString stringWithFormat:@"%02ld:%02ld", (long)minTime1,(long)secondTime1];
    if(![_timeStr isEqualToString:@""] && ![_timeStr1 isEqualToString:@""]) {
        _currentTimeLabel.text = _timeStr1;
        _wholeTimeLabel.text = _timeStr;
    }
    [TTMusicPlayBottomView playBottomView].playPercent= _slider.value;
}
#pragma  mark - 观察时间，当一首歌播完，会播放下一首
- (void)observePlay {
    if (CMTimeGetSeconds([TTMusicAVPlayer shareTTMusicAVPlayer].currentItem.currentTime) / CMTimeGetSeconds([TTMusicAVPlayer shareTTMusicAVPlayer].currentItem.duration) == 1) {
//        列表循环
        if ([TTMusicAVPlayer shareTTMusicAVPlayer].style == 0) {
            [TTMusicAVPlayer shareTTMusicAVPlayer].local += 1;
        }
//        单曲循环
        if ([TTMusicAVPlayer shareTTMusicAVPlayer].style == 1) {
        }
//        随机播放
        if ([TTMusicAVPlayer shareTTMusicAVPlayer].style == 2) {
            [TTMusicAVPlayer shareTTMusicAVPlayer].local = arc4random()%_songsModel.songs.count;
        }
        [[TTMusicAVPlayer shareTTMusicAVPlayer] playWithURL:_songsModel];
        [TTMusicAVPlayer shareTTMusicAVPlayer].style = _style;
    }
    [self refreshEverything];
}
#pragma mark - 判断数据库是否包括该数据
- (void)judge {
    
    self.dataBase = [CPYDataBaseTool dataBaseManager];


    [_dataBase openDataBase];
    [_dataBase createTable];
    if ([_dataBase selectDataBaseWithId:[NSString stringWithFormat:@"%@", [TTMusicAVPlayer shareTTMusicAVPlayer].songDetailModel.songId]]) {
        [_favoriteButton setImage:[[UIImage imageNamed:@"VCLIke"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        _isLike = YES;
    }else {
        [_favoriteButton setImage:[[UIImage imageNamed:@"VCUnLike"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        _isLike = NO;
    }
    [_dataBase closeDataBase];
}
#pragma mark - 下一曲的时候刷新页面，歌名，歌手名，图片
- (void)refreshEverything {
    _songLabel.text = [TTMusicAVPlayer shareTTMusicAVPlayer].songDetailModel.name;
    _singerLabel.text = [TTMusicAVPlayer shareTTMusicAVPlayer].songDetailModel.singerName;
    [_imageView sd_setImageWithURL:[NSURL URLWithString:[TTMusicAVPlayer shareTTMusicAVPlayer].songDetailModel.picUrl]];
    
}
- (void)loadButtonAction:(UIButton*)sender {
    
}
#pragma mark - 收藏事件
- (void)favoriteButtonAction:(UIButton *)sender {
    
//    CPYDataBaseTool *dataBase = [CPYDataBaseTool dataBaseManager];
    [_dataBase openDataBase];
    [_dataBase createTable];
    if (_isLike == YES) {
        [_dataBase deleteDataBase:[NSString stringWithFormat:@"%@", [TTMusicAVPlayer shareTTMusicAVPlayer].songDetailModel.songId]];
        [_favoriteButton setImage:[[UIImage imageNamed:@"VCUnLike"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
<<<<<<< HEAD
        [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:nil];
=======
//        协议刷新收藏列表数据
        [self.delegate reloadata];
>>>>>>> fe0a2fd4b1e06bd41939a2ff10284b46d23ab40e
        _isLike = NO;
    }else {
        [_dataBase insertDataBase:[TTMusicAVPlayer shareTTMusicAVPlayer].songDetailModel];
        [_favoriteButton setImage:[[UIImage imageNamed:@"VCLIke"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        _isLike = YES;
    }
    [_dataBase closeDataBase];
    
    
}
- (void)shareButtonAction:(UIButton *)sender {
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"570656d8e0f55a98e0000ae9"
                                      shareText:@"你要分享的文字"
                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToRenren,UMShareToDouban,nil]

                                       delegate:self];
    
}
#pragma mark - 播放MV
- (void)mvButtonAction:(UIButton *)sender {
    if ([TTMusicAVPlayer shareTTMusicAVPlayer].songDetailModel.mvList.count != 0) {
        CPYDiscoveryPageSongListModel *songDetailModel = [TTMusicAVPlayer shareTTMusicAVPlayer].songDetailModel;
        CPYDiscoveryPageUrlListModel *urlModel = songDetailModel.mvList[0];
        [[CPYMVPlayViewController shareMVPlayViewController] playWithSongMVURL:urlModel.url];
        [self presentViewController:[CPYMVPlayViewController shareMVPlayViewController] animated:YES completion:^{
        }];
    }
    else {
        [MBProgressHUD showAlertView:@"提示" andMessage:@"此歌曲MV不支持播放"];
    }
}
#pragma mark - 播放模式，列表，单曲，随机
- (void)typeButtonAction:(UIButton *)sender {
    if (_style == 2) {
        _style = 0;
    }else {
        _style += 1;
    }
//    列表
    if (_style == 0) {
        [self.typeButton setBackgroundImage:[[UIImage imageNamed:@"VCShunxu"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
//    单曲
    if (_style == 1) {
        [self.typeButton setBackgroundImage:[[UIImage imageNamed:@"VCDanqu"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
//    随机
    if (_style == 2) {
        [self.typeButton setBackgroundImage:[[UIImage imageNamed:@"VCSuiji"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
//    切换播放模式
    [TTMusicAVPlayer shareTTMusicAVPlayer].style = _style;
}
#pragma mark - 上一曲
- (void)beforeSongButtonAction {
    //  列表循环下的的上一曲
        if ([TTMusicAVPlayer shareTTMusicAVPlayer].style == 0) {
            if ([TTMusicAVPlayer shareTTMusicAVPlayer].local == 0) {
                [TTMusicAVPlayer shareTTMusicAVPlayer].local = _songsModel.songs.count - 1;
            }else {
                [TTMusicAVPlayer shareTTMusicAVPlayer].local -= 1;
            }
            [[TTMusicAVPlayer shareTTMusicAVPlayer] playWithURL:_songsModel];
            [TTMusicAVPlayer shareTTMusicAVPlayer].style = _style;
        }
//     单曲循环下的上一曲
        if ([TTMusicAVPlayer shareTTMusicAVPlayer].style == 1) {
            if ([TTMusicAVPlayer shareTTMusicAVPlayer].local == 0) {
                [TTMusicAVPlayer shareTTMusicAVPlayer].local = _songsModel.songs.count - 1;
            }else {
                [TTMusicAVPlayer shareTTMusicAVPlayer].local -= 1;
            }
            [[TTMusicAVPlayer shareTTMusicAVPlayer] playWithURL:_songsModel];
            [TTMusicAVPlayer shareTTMusicAVPlayer].style = _style;
        }
//     随机模式下的上一曲
        if ([TTMusicAVPlayer shareTTMusicAVPlayer].style == 2) {
            [TTMusicAVPlayer shareTTMusicAVPlayer].local = arc4random()%_songsModel.songs.count;
            [[TTMusicAVPlayer shareTTMusicAVPlayer] playWithURL:_songsModel];
            [TTMusicAVPlayer shareTTMusicAVPlayer].style = _style;
        }
//    刷新播放按钮的图片
    if ([TTMusicAVPlayer shareTTMusicAVPlayer].isPlay) {
        [self.playButton setBackgroundImage:[[UIImage imageNamed:@"VCStop"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
//  刷新界面数据
    [self refreshEverything];
//    判断是否收藏
    [self judge];
}
#pragma mark - 监听slider的滑动
- (void)sliderAction:(UISlider *)slider {
    //    参数1:跳转到的时间
    //    参数2:每秒多少帧（fps）
    if ([TTMusicAVPlayer shareTTMusicAVPlayer].currentItem.duration.value) {
        [[TTMusicAVPlayer shareTTMusicAVPlayer] seekToTime:CMTimeMakeWithSeconds([TTMusicAVPlayer shareTTMusicAVPlayer].currentItem.duration.value / [TTMusicAVPlayer shareTTMusicAVPlayer].currentItem.duration.timescale * self.slider.value, [TTMusicAVPlayer shareTTMusicAVPlayer].currentTime.timescale) completionHandler:^(BOOL finished) {
            [[TTMusicAVPlayer shareTTMusicAVPlayer] play];
        }];
    }
}
#pragma mark - 播放按钮点击事件
- (void)playAndStopButtonAction:(UIButton *)sender {
    if ([TTMusicAVPlayer shareTTMusicAVPlayer].isPlay) {
        [[TTMusicAVPlayer shareTTMusicAVPlayer] pause];
        [self.playButton setBackgroundImage:[[UIImage imageNamed:@"VCPlay"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [self imageRound];
        [_imageView.layer removeAllAnimations];
        [TTMusicAVPlayer shareTTMusicAVPlayer].isPlay = NO;
    } else {
        [[TTMusicAVPlayer shareTTMusicAVPlayer] play];
        [self.playButton setBackgroundImage:[[UIImage imageNamed:@"VCStop"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [self imageRound];
        [TTMusicAVPlayer shareTTMusicAVPlayer].isPlay = YES;
    }
}
#pragma mark - 下一曲
- (void)nextSongButtonAction {
    // 列表循环下的下一曲
        if ([TTMusicAVPlayer shareTTMusicAVPlayer].style == 0) {
            if ([TTMusicAVPlayer shareTTMusicAVPlayer].local == _songsModel.songs.count - 1) {
                [TTMusicAVPlayer shareTTMusicAVPlayer].local = 0;
            }else {
                [TTMusicAVPlayer shareTTMusicAVPlayer].local += 1;
            }
            [[TTMusicAVPlayer shareTTMusicAVPlayer] playWithURL:_songsModel];
            [TTMusicAVPlayer shareTTMusicAVPlayer].style = _style;
        }
//    单曲循环下的下一曲
        if ([TTMusicAVPlayer shareTTMusicAVPlayer].style == 1) {
            if ([TTMusicAVPlayer shareTTMusicAVPlayer].local == _songsModel.songs.count - 1) {
                [TTMusicAVPlayer shareTTMusicAVPlayer].local = 0;
            }else {
                [TTMusicAVPlayer shareTTMusicAVPlayer].local += 1;
            }
            [[TTMusicAVPlayer shareTTMusicAVPlayer] playWithURL:_songsModel];
            [TTMusicAVPlayer shareTTMusicAVPlayer].style = _style;
        }
//    随机模式下的下一曲
        if ([TTMusicAVPlayer shareTTMusicAVPlayer].style == 2) {
            [TTMusicAVPlayer shareTTMusicAVPlayer].local = arc4random()%_songsModel.songs.count;
            [[TTMusicAVPlayer shareTTMusicAVPlayer] playWithURL:_songsModel];
            [TTMusicAVPlayer shareTTMusicAVPlayer].style = _style;
    }
//    刷新按钮图片
    if ([TTMusicAVPlayer shareTTMusicAVPlayer].isPlay) {
        [self.playButton setBackgroundImage:[[UIImage imageNamed:@"VCStop"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];

    }
    
//    刷新页面数据
    [self refreshEverything];
//    判断是否收藏
    [self judge];
}
#pragma mark - 播放列表按钮点击事件
- (void)listButtonAction:(UIButton *)sender {
    if ([CPYListView playListView].songsModel == _songsModel) {
    }else {
        [CPYListView playListView].songsModel = _songsModel;
    }
    [self.view bringSubviewToFront:[CPYListView playListView]];
//    [self.view addSubview:[CPYListView playListView]];
    [CPYListView playListView].hidden = NO;
}
#pragma mark - 左侧按钮点击事件

- (void)leftAction:(UIBarButtonItem *)sender {
    
    [TTMusicPlayBottomView playBottomView].isPlay = [TTMusicAVPlayer shareTTMusicAVPlayer].isPlay;
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - songListModel的set方法
- (void)setSongListModel:(CPYDiscoveryPageSongListModel *)songListModel {
    _songListModel = songListModel;
    _songLabel.text = songListModel.name;
    _singerLabel.text = songListModel.singerName;
    
}
- (void)setSongsModel:(CPYSONGSModel *)songsModel {
    _songsModel = songsModel;
}
#pragma mark - 图片旋转
- (void)imageRound {
    CABasicAnimation *animation = [ CABasicAnimation
                                   animationWithKeyPath: @"transform" ];
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    
    //围绕Z轴旋转，垂直与屏幕
    animation.toValue = [ NSValue valueWithCATransform3D:
                         
    CATransform3DMakeRotation(M_PI, 0.0, 0.0, 1.0) ];
    animation.duration = 10.0;
    //旋转效果累计，先转180度，接着再旋转180度，从而实现360旋转
    animation.cumulative = YES;
    animation.repeatCount = NSIntegerMax;
    
    [self.imageView.layer setCornerRadius:CGRectGetHeight([self.imageView bounds]) / 2];
    self.imageView.layer.masksToBounds = YES;
    [self.imageView.layer addAnimation:animation forKey:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
//-(void)setBotomViewColor{
//    
//    if (_grandientLayer) {
//        [self.grandientLayer removeFromSuperlayer];
//    }
//    self.grandientLayer=[[CAGradientLayer alloc] init];
//    _grandientLayer.frame = _botomView.bounds;
//    UIColor *shadowColor=[WDGComenFunction toGrayWithColor:_showColor nearRate:0.15];
//    
//    _grandientLayer.colors=@[
//                             (id)[shadowColor colorWithAlphaComponent:0.05].CGColor,
//                             (id)[shadowColor colorWithAlphaComponent:0.65].CGColor
//                             ];
//    
//    [_botomView.layer addSublayer:_grandientLayer];
//    
//    [_slider setMinimumTrackTintColor:_showColor];
//    [_slider setMaximumTrackTintColor:[[WDGComenFunction toGrayWithColor:_showColor nearRate:0.1] colorWithAlphaComponent:0.7]];
//    
//    NSArray *allKeys= _botomBTImgs.allKeys;
//    for (NSString *key in allKeys) {
//        
//        UIImage *newIMG=[((UIImage *)_botomBTImgs[key]) imageWithTintColor:_showColor];
//        UIButton *bt=_botomBTs[key];
//        if (bt==_playBT) {
//            if ([self isPlaying]&&[key isEqualToString:@"playView-play"]) {
//                continue;
//            }
//            else if (![self isPlaying]&&[key isEqualToString:@"playView-pause"]){
//                continue;
//            }
//        }
//        if (bt==_favarateBT) {
//            if ((_isCollected&&[key isEqualToString:@"playView-favourate"])||(!_isCollected&&[key isEqualToString:@"playView-favourateyes"])) {
//                continue;
//            }
//        }
//        if (bt==_modeBT) {
//            switch (_playModel) {
//                case PlayModeLoop:
//                    if ([key isEqualToString:@"playView-loop"]) {
//                        BTNBKIMG(bt, newIMG);
//                    }
//                    break;
//                case PlayModeQueue:
//                    if ([key isEqualToString:@"playView-queue"]) {
//                        BTNBKIMG(bt, newIMG);
//                    }
//                    break;
//                case PlayModeRandom:
//                    if ([key isEqualToString:@"playView-random"]) {
//                        BTNBKIMG(bt, newIMG);
//                    }
//                    break;
//                case PlayModeSingle:
//                    if ([key isEqualToString:@"playView-single"]) {
//                        BTNBKIMG(bt, newIMG);
//                    }
//                    break;
//                default:
//                    break;
//            }
//            continue;
//        }
//        BTNBKIMG(bt, newIMG);
//    }
//    
//    //NSLog(@"%ld",_botomView.layer.sublayers.count);
//}
//-(void)storeBotomBTNs{
//    self.botomBTs=[NSMutableDictionary dictionary];
//    [_botomBTs setObject:_downloadBT forKey:@"playView-download"];
//    [_botomBTs setObject:_commentBT forKey:@"playView-comment"];
//    [_botomBTs setObject:_favarateBT forKey:@"playView-favourate"];
//    [_botomBTs setObject:_favarateBT forKey:@"playView-favourateyes"];
//    [_botomBTs setObject:_lastBT forKey:@"playView-last"];
//    [_botomBTs setObject:_manageBT forKey:@"playView-list"];
//    [_botomBTs setObject:_modeBT forKey:@"playView-loop"];
//    [_botomBTs setObject:_mvBT forKey:@"playView-mv"];
//    [_botomBTs setObject:_nextBT forKey:@"playView-next"];
//    [_botomBTs setObject:_playBT forKey:@"playView-pause"];
//    [_botomBTs setObject:_playBT forKey:@"playView-play"];
//    [_botomBTs setObject:_modeBT forKey:@"playView-queue"];
//    [_botomBTs setObject:_modeBT forKey:@"playView-random"];
//    [_botomBTs setObject:_shareBT forKey:@"playView-share"];
//    [_botomBTs setObject:_modeBT forKey:@"playView-single"];
//}
#pragma  mark - 进入页面传进来的播放状态
- (void)setStyle:(playStyle)style {
    _style = style;
    if ([TTMusicAVPlayer shareTTMusicAVPlayer].style == 0) {
        [self.typeButton setBackgroundImage:[[UIImage imageNamed:@"VCShunxu"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
    if ([TTMusicAVPlayer shareTTMusicAVPlayer].style == 1) {
        [self.typeButton setBackgroundImage:[[UIImage imageNamed:@"VCDanqu"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
    if ([TTMusicAVPlayer shareTTMusicAVPlayer].style == 2) {
        [self.typeButton setBackgroundImage:[[UIImage imageNamed:@"VCSuiji"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
}


@end
